package com.icangya.admin;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.icangya.Utils.JSONUtils;
import com.icangya.domain.*;
import com.icangya.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
/**
 * author:黄明浜
 * date:2017.10.13 PM: 5:00
 */
@CrossOrigin
@RestController
@RequestMapping(value = "/userManage")
public class UserController {

    @Autowired
    private IUserService userService;

    //查询所有用户
    @RequestMapping(value = "/findAll" , method = RequestMethod.GET)
    public JSONObject findAll(@RequestParam("pageNum")Integer pageNum){

        /*if (StringUtils.isEmpty(pageNum)){
            pageNum = 1;
        }
        if (pageNum < 1){
            return JSONUtils.getRoot(-1,"faild","当前页码不存在。");
        }*/
        if (!this.check(1L,pageNum)){
            return JSONUtils.getRoot(-1,"feild","不正确的请求参数。");
        }
        List  users = this.userService.findAll(pageNum);
        if (users == null){
            return JSONUtils.getRoot(-1,"faild","当前没有用户。");
        }
        JSONArray jsonArray=new JSONArray();
        List<User> user = (List<User>) users.get(0);
        for (User u : user) {
            JSONObject jsonObject =new JSONObject();
            jsonObject.put("user", u);
            Balance balance = this.userService.findBalanceByUserId(u.getId());
                jsonObject.put("balance", balance);
            System.out.print(u);
            jsonArray.add(jsonObject);
        }

        JSONObject object=new JSONObject();
        object.put("user",jsonArray);
        object.put("totalPage",users.get(1));
        object.put("inPage",users.get(2));
        return JSONUtils.getRoot(0,"success",object);
    }

    /*                ++++++++++++++++++   资金流水             */
    @RequestMapping(value = "/record" , method = RequestMethod.GET)
    public JSONObject findExpendRecord(@RequestParam("uid")Long uid , @RequestParam("pageNum")Integer pageNum){

        /*if (StringUtils.isEmpty(uid)){
            return JSONUtils.getRoot(-1,"feild","不正确的请求参数。");
        }
        if (uid < 0 ){
            return JSONUtils.getRoot(-1,"feild","不正确的用户ID或页码。");
        }
        if (StringUtils.isEmpty(pageNum)){
            pageNum = 1;
        }
        if (pageNum < 1){
            return JSONUtils.getRoot(-1,"feild","不正确的页码。");
        }*/
        if (!this.check(uid,pageNum)){
            return JSONUtils.getRoot(-1,"feild","不正确的请求参数。");
        }
        List<Object> expendRecords = this.userService.findByUserExpendRecord(uid , pageNum);
        if (expendRecords == null){
            return JSONUtils.getRoot(-1,"feild","没有资金流水信息或者用户不存在！");
        }
        return JSONUtils.getRoot(0,"success",expendRecords);
    }

    //充值记录
    @RequestMapping(value = "/recharge" , method = RequestMethod.GET)
    public JSONObject findRechargeRecord(@RequestParam("uid")Long uid , @RequestParam("pageNum")Integer pageNum){

        /*if (StringUtils.isEmpty(uid)){
            return JSONUtils.getRoot(-1,"feild","不正确的请求参数。");
        }
        if (uid < 0){
            return JSONUtils.getRoot(-1,"feild","不正确的用户ID。");
        }
        if (StringUtils.isEmpty(pageNum)){
            pageNum = 1;
        }
        if (pageNum < 1){
            return JSONUtils.getRoot(-1,"feild","不正确的页码。");
        }*/
        if (!this.check(uid,pageNum)){
            return JSONUtils.getRoot(-1,"feild","不正确的请求参数。");
        }
        List<Object> rechargeRecord = this.userService.findByUserRechargeRecord(uid , pageNum);

        if (rechargeRecord == null){
            return JSONUtils.getRoot(-1,"feild","没有资金充值信息或者用户不存在！");
        }

        return JSONUtils.getRoot(0,"success",rechargeRecord);
    }

    //积分明细
    @RequestMapping(value = "/pointsRecord" , method = RequestMethod.GET)
    public JSONObject findUserPointsRecord(@RequestParam("uid")Long uid , @RequestParam("pageNum")Integer pageNum){

        /*if (StringUtils.isEmpty(uid)){
            return JSONUtils.getRoot(-1,"feild","不正确的请求参数。");
        }
        if (uid < 0 ){
            return JSONUtils.getRoot(-1,"feild","不正确的用户ID或页码。");
        }
        if (StringUtils.isEmpty(pageNum)){
            pageNum = 1;
        }
        if (pageNum < 1){
            return JSONUtils.getRoot(-1,"feild","不正确的页码。");
        }*/
        if (!this.check(uid,pageNum)){
            return JSONUtils.getRoot(-1,"feild","不正确的请求参数。");
        }
        List<Object> pointsRecord = this.userService.findByUserPointsRecord(uid,pageNum);

        if (pointsRecord == null){
            return JSONUtils.getRoot(-1,"feild","没有积分明细信息或者用户不存在！");
        }

        return JSONUtils.getRoot(0,"success",pointsRecord);
    }

    //商品升级记录
    @RequestMapping(value = "/getGoodsUpgrade",method = RequestMethod.GET)
    public JSONObject getGoodsUpgradeByUserId(@RequestParam("uid") Long uid ,@RequestParam("pageNum")Integer pageNum){

        /*if (StringUtils.isEmpty(uid)){
            return JSONUtils.getRoot(-1,"feild","不正确的请求参数。");
        }
        if (uid < 0 ){
            return JSONUtils.getRoot(-1,"feild","不正确的用户ID。");
        }
        if (StringUtils.isEmpty(pageNum)) {
            pageNum = 1;
        }
        if (pageNum < 1){
            return JSONUtils.getRoot(-1,"feild","不正确的页码。");
        }*/
        if (!this.check(uid,pageNum)){
            return JSONUtils.getRoot(-1,"feild","不正确的请求参数。");
        }
        List<Object> goodsUpgradeRecords = this.userService.getGoodsUpgradeByUserIdPaging(uid , pageNum);
        if (goodsUpgradeRecords == null){
            return JSONUtils.getRoot(-1,"feild","当前没有升级记录或者用户不存在。");
        }
        return  JSONUtils.getRoot(0,"success",goodsUpgradeRecords);
    }

    //商品退货
    @RequestMapping(value = "/salesReturn" , method = RequestMethod.GET)
    public JSONObject goodSalesReturnRecord(@RequestParam("uid") Long uid ,@RequestParam("pageNum") Integer pageNum){
        /*if (StringUtils.isEmpty(uid)){
            return JSONUtils.getRoot(-1,"feild","不正确的请求参数。");
        }
        if (uid < 0 ){
            return JSONUtils.getRoot(-1,"feild","不正确的用户ID。");
        }
        if(StringUtils.isEmpty(pageNum)){
            pageNum = 1;
        }
        if (pageNum < 1){
            return JSONUtils.getRoot(-1,"feild","不正确的页码。");
        }*/
        if (!this.check(uid,pageNum)){
            return JSONUtils.getRoot(-1,"feild","不正确的请求参数。");
        }
        List<Object> goodsSalesReturnRecord = this.userService.goodSalesReturnRecord(uid,pageNum);
        if (goodsSalesReturnRecord == null){
            return JSONUtils.getRoot(-1,"feild","当前没有退货记录或者用户不存在。");
        }
        return  JSONUtils.getRoot(0,"success",goodsSalesReturnRecord);
    }

    //订单明细
    @RequestMapping(value = "/findOrderByUserId" , method = RequestMethod.GET)
    public JSONObject getOrdersByUserId(@RequestParam("uid") Long uid ,@RequestParam("pageNum") Integer pageNum){
        /*if (StringUtils.isEmpty(uid)){
            return JSONUtils.getRoot(-1,"feild","不正确的请求参数。");
        }
        if (uid < 0 ){
            return JSONUtils.getRoot(-1,"feild","不正确的用户ID。");
        }
        if(StringUtils.isEmpty(pageNum)){
            pageNum = 1;
        }
        if (pageNum < 1){
            return JSONUtils.getRoot(-1,"feild","不正确的页码。");
        }*/
        if (!this.check(uid,pageNum)){
            return JSONUtils.getRoot(-1,"feild","不正确的请求参数。");
        }
        List<Object> ordersByUserId = this.userService.findOrderByUserId(uid,pageNum);
        if (ordersByUserId == null){
            return JSONUtils.getRoot(-1,"feild","当前没有订单信息记录或者用户不存在。");
        }
        return  JSONUtils.getRoot(0,"success",ordersByUserId);
    }

    //红包拆分明细
    @RequestMapping(value = "/findRedPacketRecord" , method = RequestMethod.GET)
    public JSONObject findRedPacketRecordByUserId(@RequestParam("uid") Long uid ,@RequestParam("pageNum") Integer pageNum){
        /*if (StringUtils.isEmpty(uid)){
            return JSONUtils.getRoot(-1,"feild","不正确的请求参数。");
        }
        if (uid < 0 ){
            return JSONUtils.getRoot(-1,"feild","不正确的用户ID。");
        }
        if(StringUtils.isEmpty(pageNum)){
            pageNum = 1;
        }
        if (pageNum < 1){
            return JSONUtils.getRoot(-1,"feild","不正确的页码。");
        }*/
        if (!this.check(uid,pageNum)){
            return JSONUtils.getRoot(-1,"feild","不正确的请求参数。");
        }
        List<Object> findRedPacketRecord = this.userService.findRedPacketRecordByUserId(uid,pageNum);
        if (findRedPacketRecord == null){
            return JSONUtils.getRoot(-1,"feild","当前没有红包拆分记录或者用户不存在。");
        }
        return  JSONUtils.getRoot(0,"success",findRedPacketRecord);
    }

    //银行卡信息
    @RequestMapping(value = "/findBankCar", method = RequestMethod.GET)
    public JSONObject findBankCarByUserId(@RequestParam("uid")Long uid){
        /*if (StringUtils.isEmpty(uid)){
            return JSONUtils.getRoot(-1,"feild","不正确的请求参数。");
        }
        if (uid < 0 ){
            return JSONUtils.getRoot(-1,"feild","不正确的用户ID。");
        }*/
        if (!this.check(uid,1)){
            return JSONUtils.getRoot(-1,"feild","不正确的请求参数。");
        }
        List<RwUserBank> userBanks = this.userService.findBankCarByUserId(uid);
        if (userBanks == null){
            return JSONUtils.getRoot(-1,"feild","没有绑定银行卡信息或用户ID有误。");
        }
        return  JSONUtils.getRoot(0,"success",userBanks);
    }

    //查询猜奇偶明细
    @RequestMapping(value = "/findOdevity" , method = RequestMethod.GET)
    public JSONObject findOdevityByUserId(@RequestParam("uid") Long uid ,@RequestParam("pageNum") Integer pageNum){
        /*if (StringUtils.isEmpty(uid)){
            return JSONUtils.getRoot(-1,"feild","不正确的请求参数。");
        }
        if (uid < 0 ){
            return JSONUtils.getRoot(-1,"feild","不正确的用户ID。");
        }
        if(StringUtils.isEmpty(pageNum)){
            pageNum = 1;
        }
        if (pageNum < 1){
            return JSONUtils.getRoot(-1,"feild","不正确的页码。");
        }*/
        if (!this.check(uid,pageNum)){
            return JSONUtils.getRoot(-1,"feild","不正确的请求参数。");
        }
        List<Object> odevitys = this.userService.findOdevityByUserId(uid,pageNum);
        if (odevitys == null){
            return JSONUtils.getRoot(-1,"feild","当前没有猜奇偶明细或者用户不存在。");
        }
        return  JSONUtils.getRoot(0,"success",odevitys);
    }

    //查询团队明细
    @RequestMapping(value = "/findLevel", method = RequestMethod.GET)
    public JSONObject findLevel(@RequestParam("uid")Long uid){
        /*if (StringUtils.isEmpty(uid)){
            return JSONUtils.getRoot(-1,"feild","不正确的请求参数。");
        }
        if (uid < 0 ){
            return JSONUtils.getRoot(-1,"feild","不正确的用户ID。");
        }*/
        if (!this.check(uid,1)){
            return JSONUtils.getRoot(-1,"feild","不正确的请求参数。");
        }
        Long[] level = this.userService.findUserLevelByUserId(uid);
        if (level.length == 0){
            return JSONUtils.getRoot(-1,"feild","没有绑定银行卡信息或用户ID有误。");
        }
        JSONArray levelRecord = new JSONArray();
        long sum = 0L;
        for (int i = 0 ; i < level.length ; i++){
            JSONObject jsonObject = new JSONObject();
            jsonObject.put(i+1+"级代理",level[i]);
            levelRecord.add(jsonObject);
            sum += level[i];
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("代理总人数",sum);
        levelRecord.add(jsonObject);

        return  JSONUtils.getRoot(0,"success",levelRecord);
    }

    //用户实名信息
    @RequestMapping(value = "/findUserReal" , method = RequestMethod.GET)
    public JSONObject findByUserReal(@RequestParam("uid") Long uid){
        /*if (StringUtils.isEmpty(uid)){
            return JSONUtils.getRoot(-1,"feild","不正确的请求参数。");
        }*/
        if (!this.check(uid,1)){
            return JSONUtils.getRoot(-1,"feild","不正确的请求参数。");
        }
        User user = this.userService.findByUserReal(uid);
        if (user == null){
            return JSONUtils.getRoot(-1,"feild","");
        }
        return  JSONUtils.getRoot(0,"success",user);
    }

    /**
     * 通用检测数据安全
     * @param uid
     * @param pageNum
     * @return
     */
    public boolean check(Long uid , Integer pageNum){
        boolean success = false;
        if (uid != 1L){
            if (StringUtils.isEmpty(uid)){
                return success;
            }
            if (uid < 0 ){
                return success;
            }
        }
        if (pageNum == null ){
            return success;
        }
        if ( pageNum != 1){
            if (pageNum != null){
                if (pageNum < 1){
                    return success;
                }
            }
        }
        success = true;
        return success;
    }

    @RequestMapping(value = "/errorPage" ,method = RequestMethod.GET)
    public JSONObject errorPage(){
        return JSONUtils.getRoot(-1,"feild","无权访问！");
    }
}
